7. Compression
You can enable compression on
tables and indexes in SQL Server 2008 to reduce the disk space needed to
store the data in those tables and indexes. Table and index compression
is only available in the Enterprise and Developer Editions of SQL
Server. You can implement two different levels of compression: row-level
compression and page-level compression.
Row-level compression
works by reducing the metadata required to store the data itself.
Row-level compression also reduces the amount of storage by using
variable length storage for fixed length data types, including numeric
data types. For example, a column with a data type int
may require 1 byte to store the number 1 and 2 bytes to store the
number 1,000 instead of requiring 4 bytes of storage for every single
value.
Page-level compression
works by implementing row-level compression along with two other
techniques called prefix compression and dictionary compression. Prefix compression
works by identifying a common value for the data in each column that
can be used to reduce storage requirements. The value is then replaced
by a pointer to the value that is stored in the compression information
structure stored immediately after the page header. Dictionary compression
also replaces values on the page with a pointer to a common value in
the compression information structure. The main difference is that
dictionary compression is not limited to a single column; it looks for
common values on the entire page. Page compression does not occur
immediately, as there would be no benefit in compressing a nearly empty
page.
There are a few things to
keep in mind when using data compression. Data compression requires less
input and output (IO) because it requires fewer disk reads to access
the data. However, data compression will increase the CPU because SQL
Server now has to compress and uncompress the data. Page-level
compression provides a higher compression ratio than row-level
compression, but you may see higher levels of CPU utilization when using
pagelevel compression. Deciding whether to use row-level, page-level,
or no compression at all is entirely dependent on how each option
affects your environment. You could see drastic space savings and
increased performance by enabling compression; however, if you are
already CPU bound, you may hinder performance by enabling compression.
You can use the sp_estimate_data_compression_savings stored procedure to determine the estimated space you will save by enabling row or page compression, as shown in Figure 4.
To create a compressed
table, you need to specify the compression type (row or page) you would
like following the table definition. Listing 20 shows the correct syntax to create a table with page compression.
Example 20. Syntax to Enable Page Compression When Creating a Table
USE AdventureWorks2008 GO
--Drop the table if it currently exists IF OBJECT_ID('dbo.CustInfo', 'U') IS NOT NULL DROP TABLE dbo.CustInfo;
--Create the table with page compression CREATE TABLE Orders (OrderID int identity, OrderDate DateTime NOT NULL) WITH (DATA_COMPRESSION = PAGE)
|
To add compression or change the existing compression on a table, you can use the ALTER TABLE statement to rebuild the current pages with the selected compression type. Listing 21
changes the compression to row-level compression for the Orders table.
To remove compression, all you need to do is change the value to DATA_COMPRESSION = NONE.
Example 21. Syntax to Add or Change the Compression on an Existing Table
ALTER TABLE Orders
REBUILD WITH (DATA_COMPRESSION = ROW)